package io.filecopy;

import java.io.File;
import java.io.IOException;

public class DeleteTask implements FileSystemTask {

  private File file;

  public DeleteTask(File file) {
    this.file = file;
  }

  @Override
  public void execute(ProgressLogger progress) throws IOException {
    long size = file.length();
    boolean deleted = file.delete();
    if (deleted) {
      progress.fileDeleted(size);
    } else {
      progress.fileDeletionFailed(size);
      throw new IOException("Object can not be deleted: "
          + file.getAbsolutePath());
    }
  }

  @Override
  public String toString() {
    return "delete \"" + file.getAbsolutePath() + "\"";
  }
}